/* 微信支付js */
module.exports = {
  wxpay: wxpay,
  wxpays: wxpays
}

const app = getApp();
/**
 * 
 * orderInfo 参数信息
 * func 回调函数
 */
function wxpay(that, orderInfo,func,failFunc) {
  let msg = '';
  if (orderInfo == undefined || orderInfo == null || orderInfo==''){
    msg += '参数：orderInfo为undefined或者null!';
  }else{
    if (orderInfo.name == null || orderInfo.name == undefined) {
      msg += '参数：orderInfo.name为undefined或者null!\n';
    }
    if (orderInfo.bh == null || orderInfo.bh == undefined) {
      msg += '参数：orderInfo.bh为undefined或者null!\n';
    }
    if (orderInfo.total_fee == null || orderInfo.total_fee == undefined) {
      msg += '参数：orderInfo.total_fee为undefined或者null!\n';
    }
    if (orderInfo.uid == null || orderInfo.uid == undefined) {
      msg += '参数：orderInfo.uid为undefined或者null!\n';
    }
    if (orderInfo.openid == null || orderInfo.openid == undefined) {
      msg += '参数：orderInfo.openid为undefined或者null!\n';
    }
  }
  if(msg==''){
    generateOrder(that, orderInfo, func, failFunc);
  } else {
    console.log('参数错误！');
    console.error('wxpay:'+msg)
    typeof failFunc == "function" && failFunc(res);
  }
}

/**生成商户订单 */
function generateOrder(that, orderInfo, func, failFunc) {
  var param = {
    body: orderInfo.name,
    attach: orderInfo.bh,
    total_fee: parseInt(orderInfo.total_fee * 10 * 10),
    openId: orderInfo.openid,
    uid: orderInfo.uid,
    appId: app.globalData.appid
  };
  console.log(param)
  app.postRequest('/api/pay.jspx', param,function(res){
    console.log("统一支付");
    console.log(res);
    var s = res.data;
      var d = s.length;
      var pay = res.data;
      //发起支付
      var timeStamp = pay[d - 1].timeStamp;
      var packages = pay[d - 1].package;
      var paySign = pay[d - 1].paySign;
      var nonceStr = pay[d - 1].nonceStr;
      var out_trade_no = pay[d - 1].out_trade_no;

      if (pay[d - 1].return_code == 'SUCCESS') {
        var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5",
         "nonceStr": nonceStr, 'out_trade_no': out_trade_no };
        pays(that, param, func, failFunc)
      } else {
        //补偿措施
        wx.showModal({
          content: '支付失败',
          showCancel: false
        })
        typeof failFunc == "function" && failFunc(res);
      }

  });
}


/* 支付   */
function pays(that, param, func,failFunc) {
  var that = this;
  console.log("支付")
  var state = 0;
  wx.requestPayment({
    timeStamp: param.timeStamp,
    nonceStr: param.nonceStr,
    package: param.package,
    signType: param.signType,
    paySign: param.paySign,
    success: function (res) {
      console.log('支付成功')
      typeof func == "function" && func(res, param);
    },
    complete: function (res) {
      console.log("支付complete")
      console.log(res)
      var msg = '';
      if (res.errMsg.indexOf('fail') > -1) { 
        msg ='取消支付';
      }
      if (msg != '') {
        typeof failFunc == "function" && failFunc(res);
        // wx.showModal({
        //   content: msg,
        //   showCancel: false
        // })
      }
    }
  })
  console.log("支付结束")
}

function wxpays(that, param, func, failFunc) {
  var that = this;
  console.log("支付",param)
  var state = 0;
  wx.requestPayment({
    timeStamp: param.timeStamp,
    nonceStr: param.nonceStr,
    package: param.package,
    signType: param.signType,
    paySign: param.paySign,
    success: function (res) {
      console.log('支付成功')
      typeof func == "function" && func(res, param);
    },
    complete: function (res) {
      console.log("支付complete")
      console.log(res)
      var msg = '';
      if (res.errMsg.indexOf('fail') > -1) {
        msg = '取消支付';
      }
      if (msg != '') {
        typeof failFunc == "function" && failFunc(res);
        // wx.showModal({
        //   content: msg,
        //   showCancel: false
        // })
      }
    }
  })
  console.log("支付结束")
}
/**
 * 使用案例
 * const pay = require('../../utils/pay.js');
 *  var openid = wx.getStorageSync('openid');
    pay.wxpay(this, { name: '充值', bh: date.getDate(), total_fee: 1, uid: 282, openid: openid},function(res){
      console.log(res);
    });
 */